package com.guigu.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.ManufactureDesignProcedureDetailsDto;
import com.guigu.erp.mapper.ManufactureDesignProcedureDetailsMapper;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.pojo.ManufactureDesignProcedureDetails;
import com.guigu.erp.pojo.ManufactureDesignProcedureModule;
import com.guigu.erp.service.ManufactureDesignProcedureDetailsService;
import com.guigu.erp.service.ManufactureDesignProcedureModuleService;
import com.guigu.erp.service.ManufactureDesignProcedureService;
import com.guigu.erp.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class ManufactureDesignProcedureDetailsServiceImpl extends ServiceImpl<ManufactureDesignProcedureDetailsMapper,ManufactureDesignProcedureDetails>
        implements ManufactureDesignProcedureDetailsService {

    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;

    @Override
    public PageUtils<ManufactureDesignProcedureDetails> procedureDetailsPage(Integer id, int pageNo, int pageSize) {
        QueryWrapper wrapper=new QueryWrapper();
        //额外加1个条件
        //父级id
        System.out.println("id:"+id);
        wrapper.eq("parent_id",id);
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedureDetails> manufactureDesignProcedureDetails=this.list(wrapper);
        PageInfo<ManufactureDesignProcedureDetails> pageInfo=new PageInfo<ManufactureDesignProcedureDetails>(manufactureDesignProcedureDetails);

        //把PageHelper PageUtils
        PageUtils<ManufactureDesignProcedureDetails> pageUtils=new PageUtils<ManufactureDesignProcedureDetails>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        System.out.println(pageUtils);
        return pageUtils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedureDetails> detail(Integer parentId, int pageNo, int pageSize) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",parentId);
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedureDetails> list=this.list(wrapper);
        PageInfo<ManufactureDesignProcedureDetails> pageInfo=new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedureDetails> pageUtils=new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    /**
     * 添加工序物料和明细
     * @param manufactureDesignProcedureDetailsDto
     */
    @Override
    public void makeModule(ManufactureDesignProcedureDetailsDto manufactureDesignProcedureDetailsDto) {
        this.updateById(manufactureDesignProcedureDetailsDto);
        for (int i=0;i<manufactureDesignProcedureDetailsDto.getList().size();i++){
            System.out.println(i);
            DesignModuleDetails details = manufactureDesignProcedureDetailsDto.getList().get(i);
            ManufactureDesignProcedureModule procedureModule=new ManufactureDesignProcedureModule();
            procedureModule.setParentId(manufactureDesignProcedureDetailsDto.getId());
            procedureModule.setSubtotal(details.getSubtotal());
            procedureModule.setAmount(details.getAmount());
            procedureModule.setCostPrice(details.getCostPrice());
            procedureModule.setProductId(details.getProductId());
            procedureModule.setProductName(details.getProductName());
            procedureModule.setAmountUnit(details.getAmountUnit());
            procedureModule.setDetailsNumber(details.getDetailsNumber());
            procedureModule.setProductDescribe(details.getProductDescribe());
            if(procedureModule.getAmount()!=null){
                manufactureDesignProcedureModuleService.save(procedureModule);
            }
        }
    }

    /**
     * 产品生产工序明细查询通过设计编号查询
     * @param id
     * @return
     */
    @Override
    public List<ManufactureDesignProcedureDetails> detailsById(int id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<ManufactureDesignProcedureDetails> details=this.list(wrapper);
        return details;
    }
}
